<?php

/**
 * MVM_MALL 网上商店系统  会员管理
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-07-11 $
 * $Id: member.inc.php www.mvmmall.com$
 * ---------------------------------------------
*/

if(!defined('MVMMALL') || !defined('IN_ADMINCP')) exit('Access Denied');
if($action=='list'){
	require_once MVMMALL_ROOT.'./include/pager.class.php';
	$serch_sql       = false;
	if($amode == 'detail_search') {
		$serch_sql   = $ps_duse != '1' ? " WHERE register_date >= '".time_format($start_time). "'AND register_date <= '".time_format($end_time)."'" : 'WHERE register_date > 0';
		$ps_duse==1 && $duse_check = 'checked';
		$ps_sex==1  && $sex_wan    ='checked';
		$ps_sex==2  && $sex_man    ='checked';
		$ps_name1   && $serch_sql .= "  AND member_name LIKE '%".filtering($ps_name1)."%'";
		$ps_member  && $serch_sql .= "  AND member_id LIKE '%".filtering($ps_member)."%'";
		$ps_email   && $serch_sql .= "  AND member_email LIKE '%".filtering($ps_email)."%'";
		$ps_sex     && $serch_sql .= "  AND member_sex = '".filtering($ps_sex)."'";
		$ps_tel     && $serch_sql .= "  AND member_tel1 LIKE '%".filtering($ps_tel)."%'";
		$ps_jumin   && $serch_sql .= "  AND member_jumin = '".filtering($ps_jumin)."'";
		$ps_address && $serch_sql .= "  AND member_address LIKE '%".filtering($ps_address)."%'";
		$ps_name2   && $serch_sql .= "  AND member_recommend LIKE '%".filtering($ps_name2)."%'";
		$province   && $serch_sql .= "  AND province = '".filtering($province)."'";
		$city       && $serch_sql .= "  AND city = '".filtering($city)."'";
	}
	$total_count = $db->counter("{$tablepre}member_table",$serch_sql);
	$grade       = $cache->get_cache('grade');//会员等级
	$page        = $page ? (int)$page:1;
	$list_num    = 10;
	$rowset      = new Pager($total_count,$list_num,$page);
	$from_record = $rowset->_offset();
	$result      = $db->query("SELECT * FROM `{$tablepre}member_table` $serch_sql ORDER BY uid DESC LIMIT $from_record, $list_num");
	while($rt    = $db->fetch_array($result)) {
		$rt['register_date'] = date($mm_time_format,$rt['register_date']);
		$rt['modify_date']   = date($mm_time_format,$rt['modify_date']);
		$rt['member_money']  = currency($rt['member_money']);
		$rt['member_class']  = $grade[$rt['member_class']];
		$rt['member_sex']    = $rt['member_sex']==1 ? $lang['man'] :$lang['women'];
		$rt['adminid']       = $rt['adminid']   ==1 ? $lang['admin_group'] :$lang['general_group'];
		$rt['edit']          = "admincp.php?module=$module&action=edit&uid=$rt[uid]";
		$rt['del']           = "admincp.php?module=$module&action=del&uid=$rt[uid]";
		$member_rt[]         = $rt;
	}
	$page_list = $rowset->link("admincp.php?module=$module&action=$action&amode=$amode&start_mm=$start_mm&start_dd=$start_dd&start_yy=$start_yy&end_mm=$end_mm&end_dd=$end_dd&end_yy=$end_yy&ps_duse=$ps_duse&ps_name1=$ps_name1&ps_jumin=$ps_jumin&ps_email=$ps_email&ps_member=$ps_member&ps_sex=$ps_sex&ps_tel=$ps_tel&ps_address=$ps_address&ps_name2=$ps_name2&province=$province&city=$city&page=");
	require_once template('member');
	footer();
} elseif($action=='add'){
	if($setp==1&&$_POST){
		!$login_id && show_msg('intput_member');
		(strlen($login_id)<4  || strlen($login_id)>15) && show_msg('member_check');;
		!$pass1    && show_msg('password_require');
		$pass1    != $pass2 && show_msg('password_rqual');
		$login_id  = dhtmlchars($login_id);
		$email     = dhtmlchars($email);
		$birthday  = $birth_yy.sprintf("%02d",$birth_mm).sprintf("%02d",$birth_dd);
		$pass1     = md5($pass1);
		if(!ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$",$email)){
			show_msg('email_err');
		}
		foreach($char_key as $value){
			strpos($login_id,$value)!==false && show_msg('user_err');
		}
		$rt_user = $db->get_one("SELECT member_id FROM `{$tablepre}member_table` WHERE member_id = '$login_id'");
		$rt_user['member_id'] && show_msg('user_mudb');
		$rt_email = $db->get_one("SELECT member_email FROM `{$tablepre}member_table` WHERE member_email = '$email'");
		$rt_email['member_email'] && show_msg('user_email');;
		//头像
		if ($_FILES['member_file']['name']!=''){
			require_once MVMMALL_ROOT.'./include/upfile.class.php';
			$rowset = new upfile('gif,jpg,png,bmp','images/member/');
			$member_file_text = $rowset->upload('member_file');
		}
		$rows = array(
		'member_class'        => $mclass,
		'member_id'           => $login_id,
		'member_pass'         => $pass1,
		'member_name'         => $name,
		'member_jumin'        => $jumin,
		'member_sex'           => $sex,
		'member_birthday'     => $birthday,
		'member_tel1'         => $tel1,
		'member_tel2'         => $tel2,
		'member_email'        => $email,
		'member_automail'     => $automail,
		'member_zip'          => $zip1,
		'province'            => $province,
		'city'                => $city,
		'member_address'      => $address1,
		'member_homepage'     => $homepage,
		'icq'                 => $icq,
		'qq'                  => $qq,
		'yahoo'               => $yahoo,
		'msn'                 => $msn,
		'taobao'              => $taobao,
		'member_automail'     => $automail,
		'member_recommend'    => $member_recommend,
		'member_introduction' => $intro,
		'member_point'        => $member_point,
		'member_money'        => $member_money,
		'member_image'        => $member_file_text,
		'member_login'        => 0,
		'modify_ip'           => $m_user_ip,
		'modify_date'         => $m_now_time,
		'adminid'             => $adminid,
		'register_date'       => $m_now_time,
		'member_admin_memo'   => $member_admin_memo,
		'myinfo'              => $userid
		);
		$insert_id = $db->insert("{$tablepre}member_table",dhtmlchars($rows));
		//写入默认收货地址
		$address_row = array(
		'consignee' => $name,
		'email'     => $email,
		'tel'       => $tel1,
		'mobile'    => $tel2,
		'zipcode'   => $zip,
		'province'  => $province,
		'city'      => $city,
		'address'   => $address1,
		'member_id' => $insert_id
		);
        $db->insert("{$tablepre}address",dhtmlchars($address_row));
    
        if($supply_class=='6'){
            $rs_member = $db->fetch_array($db->query("SELECT supply_id FROM {$tablepre}supply_table WHERE supply_id = '$old_id'"));
            if(!$rs_member[supply_id]){
                $query = "INSERT INTO {$tablepre}supply_table set
	                   supply_id              = '$id',
					   supply_class           = '6',
					   supply_company         = '$name',
					   register_date          = '$m_now_time'";
                $db->query($query);
            }
        }
        admin_msg('success','admincp.php?module=member&action=list');
    } else {
    	$grade_select = drop_menu($cache->get_cache('grade'),'mclass');
    	drop_check(0,'adminid');
        require_once template('member_add');
        footer();
    }
} elseif ($action=='edit'&& is_numeric($uid)) {
    if($setp==1&&$_POST){
    	$rt_member = $db->get_one("SELECT * FROM `{$tablepre}member_table` WHERE uid='$uid'");
    	if ($pass1){
    		$pass1 != $pass2 && show_msg('password_rqual');
    		$pass1  = md5($pass1);
    	}else {
    		$pass1 = $rt_member['member_pass'];;
    	}
    	if(!ereg("^[-a-zA-Z0-9_\.]+\@([0-9A-Za-z][0-9A-Za-z-]+\.)+[A-Za-z]{2,5}$",$email)){
    		show_msg('email_err');
    	}
    	//处理修改邮件
    	if ($rt_member['member_email']!=$email){
    		$rs_email = $db->get_one("SELECT member_email FROM `{$tablepre}member_table` WHERE `member_email` = '$email'");
    		$rs_email['member_email'] && show_msg('user_email');
    	}
    	//处理修改用户名
    	if ($rt_member['member_id']!=$login_id) {
    	    (strlen($login_id)<4  || strlen($login_id)>15) && show_msg('member_check');;
    	    foreach($char_key as $value){
    		strpos($login_id,$value)!==false && show_msg('user_err');
    	    }
    	   $rt_user = $db->get_one("SELECT member_id FROM `{$tablepre}member_table` WHERE member_id = '$login_id'");
    	   $rt_user['member_id'] && show_msg('user_mudb');
    	}
    	$birthday  = $birth_yy.sprintf("%02d",$birth_mm).sprintf("%02d",$birth_dd);
    	
         //头像
         if ($_FILES['member_file']['name']!=''){
            require_once MVMMALL_ROOT.'./include/upfile.class.php';
            file_unlink($member_file_text);//删除原头像
            $rowset = new upfile('gif,jpg,png,bmp','images/member/');
			$member_file_text = $rowset->upload('member_file');
        }
        $member_money = floatval($rt_member['member_money']+ $new_money);
        $member_point = floatval($rt_member['member_point']+ $new_point);
       $rows = array(
		'member_class'        => $mclass,
		'member_id'           => $login_id,
		'member_pass'         => $pass1,
		'member_name'         => $name,
		'member_jumin'        => $jumin,
		'member_sex'           => $sex,
		'member_birthday'     => $birthday,
		'member_tel1'         => $tel1,
		'member_tel2'         => $tel2,
		'member_email'        => $email,
		'member_automail'     => $automail,
		'member_zip'          => $zip1,
		'province'            => $province,
		'city'                => $city,
		'member_address'      => $address1,
		'member_homepage'     => $homepage,
		'icq'                 => $icq,
		'qq'                  => $qq,
		'yahoo'               => $yahoo,
		'msn'                 => $msn,
		'taobao'              => $taobao,
		'member_automail'     => $automail,
		'member_recommend'    => $member_recommend,
		'member_introduction' => $intro,
		'member_point'        => $member_point,
		'member_money'        => $member_money,
		'member_image'        => $member_file_text,
		'modify_ip'           => $m_user_ip,
		'modify_date'         => $m_now_time,
		'adminid'             => $adminid,
		'register_date'       => $m_now_time,
		'member_admin_memo'   => $member_admin_memo,
		'myinfo'              => $userid
		);
		$db->update("{$tablepre}member_table",dhtmlchars($rows),"uid='$uid'");
        if($new_point) {
            !$new_point_text && $new_point_text = $lang['point_admin_msg'];
            $query = "INSERT INTO `{$tablepre}point_table` SET
                      point_id       = '$login_id',
                      point_add      = '$new_point',
                      point_reason   = '$new_point_text',
                      modify_id      = '$m_check_id',
                      modify_ip      = '$m_user_ip',
                      point_category = '1',
                      register_date  = '$m_now_time',
                      approval_date  = '$m_now_time' ";
            $db->query($query);
        }
        if($new_money) {
            !$new_money_text && $new_money_text = $lang['money_admin_msg'];
            $query = "INSERT INTO `{$tablepre}money_table` SET
                      money_id       = '$login_id',
                      money_add      = '$new_money',
                      money_reason   = '$new_money_text',
                      modify_id      = '$m_check_id',
                      modify_ip      = '$m_user_ip',
                      money_category = '0',
                      register_date  = '$m_now_time',
                      approval_date  = '$m_now_time' ";
            $db->query($query);
        }
      admin_msg('success','admincp.php?module=member&action=list');
    } else {
        $uid      = (int)$uid;
        $user_rt  = $db->get_one("SELECT * FROM `{$tablepre}member_table` WHERE uid = '$uid'");
        $zip1     = substr($user_rt['member_zip'],0,6);
        $jumin    = substr($user_rt['member_jumin'],0,18);
        $birth_yy = substr($user_rt['member_birthday'],0,4);
        $birth_mm = substr($user_rt['member_birthday'],4,2);
        $birth_dd = substr($user_rt['member_birthday'],6,2);
        $lang['add_member']          = $lang['editor_member'].$user_rt['member_id'];
        @extract($user_rt,EXTR_OVERWRITE);
        drop_check($adminid,'adminid');
        drop_check($member_sex,'member_sex');
        drop_check($member_automail,'automail');
        //调用收货地址列表
        $rt_address=array();
        $result=$db->query("SELECT * FROM `{$tablepre}address` WHERE member_id='$uid'");
        while ($rt=$db->fetch_array($result)){
        	$rt['address'] = $rt['province'].' '.$rt['city'].' '.$rt['address'];
            $rt_address[]=$rt;
        }
        //等级下拉菜单；
        $grade_select = drop_menu($cache->get_cache('grade'),'mclass',$member_class);
        require_once template('member_add');
        footer();
    }
} elseif ($action=='del' && is_numeric($uid)) {
   $rt_member = $db->get_one("SELECT uid,member_image,member_id FROM `{$tablepre}member_table` WHERE uid = '$uid'");
    if($rt_member['uid']==1) {
        admin_msg('admin_delete');
    } elseif($rt_member['uid']) {
    	$rt_member['member_image'] && file_unlink($rt_member['member_image']);//删除头像
    	$db->query("DELETE FROM `{$tablepre}member_table` WHERE uid = '$uid'");
    	$db->query("DELETE FROM `{$tablepre}point_table` WHERE point_id = '$uid'");
    	$db->query("DELETE FROM `{$tablepre}money_table` WHERE money_id = '$uid'");
    }else {
    	admin_msg('pass_worng');
    }
   admin_msg('success',dreferer());
} else {
    admin_msg('pass_worng');
}